<template>
  <div>
    <el-row :gutter="24">
      <el-col :span="10" :offset="6">
        <div class="form-box">
          <div class="welcome">欢迎登录</div>
          <el-form
            ref="form"
            :model="form"
            :rules="rules"
            label-width="60px"
            hide-required-asterisk
          >
            <el-form-item label="邮箱" prop="email">
              <el-input
                class="email"
                v-model="form.email"
                placeholder="请输入邮箱"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                class="password"
                v-model="form.password"
                placeholder="请输入密码"
                show-password
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                class="submit-btn"
                type="primary"
                @click="submitForm('form')"
                >登录</el-button
              >
            </el-form-item>
            <div class="tool">
              <el-link>忘记密码？</el-link>
              <el-link type="primary"
                ><router-link tag="span" to="register"
                  >去注册</router-link
                ></el-link
              >
            </div>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="less" scoped>
.form-box {
  text-align: center;
  background-color: #fff;
  padding: 50px 40px 50px 10px;
  border: 1px solid #ddd;
  margin-top: 100px;
  .welcome {
    font-size: 25px;
    margin-bottom: 20px;
  }
  .submit-btn {
    width: 100%;
  }
  .tool {
    margin-left: 50px;
    display: flex;
    justify-content: space-between;
  }
}
</style>

<script>
import { mapMutations } from 'vuex'
import apiUrl from '../utils/apis'
export default {
  data() {
    return {
      form: {
        email: '',
        password: '',
      },
      rules: {
        email: [{ required: true, type: 'email', message: '请输入邮箱' }],
        password: [{ required: true, message: '请输入密码' }],
      },
    }
  },
  methods: {
    ...mapMutations(['changeToken', 'changeUser']),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let that = this
          console.log(that.form)
          that.$axios
            .post(apiUrl.login, {
              email: that.form.email,
              password: that.form.password,
            })
            .then((res) => {
              console.log(res)
              if (res.success) {
                that.changeToken('Bearer ' + res.token)
                that.changeUser(JSON.stringify(res.data))
                that.$router.push('index')
              }
            })
            .catch((err) => {
              console.log(err.response)
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>
